<template>
  <div class="app-container">
    <div class="search-box">
      <div class="search-hd">项目管理</div>
      <div class="search-bd">
        <!-- 新增项目的dialog对话框 Form -->
        <el-button type="success" @click="addProjectPre()" size="mini"
          >新增</el-button
        >
        <el-dialog title="新增项目" :visible.sync="dialogFormVisible">
          <el-form :model="form">
            <el-form-item label="项目名称" :label-width="formLabelWidth">
              <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="应用服务器" :label-width="formLabelWidth">
              <el-input v-model="form.server" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="数据库" :label-width="formLabelWidth">
              <el-input v-model="form.db" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="定时任务" :label-width="formLabelWidth">
              <el-input v-model="form.mission" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="业务监控" :label-width="formLabelWidth">
              <el-input v-model="form.service" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="项目编号" :label-width="formLabelWidth">
              <el-input v-model="form.number" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="服务商" :label-width="formLabelWidth">
              <el-input v-model="form.provider" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="联系方式" :label-width="formLabelWidth">
              <el-input
                v-model="form.tel"
                autocomplete="off"
                placeholder="格式：xxx@163.com (多个用逗号分隔)"
              ></el-input>
            </el-form-item>
            <el-form-item label="项目地址" :label-width="formLabelWidth">
              <el-input
                v-model="form.address"
                autocomplete="off"
                placeholder="格式:   192.168.200.129:8080  英文标点符号"
              >
              </el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addProject(form)"
              >确 定</el-button
            >
          </div>
        </el-dialog>
        <!-- 修改项目的dialog对话框 Form -->
        <el-dialog title="修改项目" :visible.sync="dialogUpdateFormVisible">
          <el-form :model="form">
            <el-form-item label="项目名称" :label-width="formLabelWidth">
              <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="应用服务器" :label-width="formLabelWidth">
              <el-input v-model="form.server" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="数据库" :label-width="formLabelWidth">
              <el-input v-model="form.db" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="定时任务" :label-width="formLabelWidth">
              <el-input v-model="form.mission" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="业务监控" :label-width="formLabelWidth">
              <el-input v-model="form.service" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="项目编号" :label-width="formLabelWidth">
              <el-input v-model="form.number" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="服务商" :label-width="formLabelWidth">
              <el-input v-model="form.provider" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="联系方式" :label-width="formLabelWidth">
              <el-input v-model="form.tel" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="项目地址" :label-width="formLabelWidth">
              <el-input
                v-model="form.address"
                autocomplete="off"
                placeholder="格式:   192.168.200.129:8080  英文标点符号"
              >
              </el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogUpdateFormVisible = false"
              >取 消</el-button
            >
            <el-button type="primary" @click="updateProject(form)"
              >确 定</el-button
            >
          </div>
        </el-dialog>
      </div>

      <!--项目展示模块-->
    </div>
    <div class="main-box">
      <div class="main-hd">项目管理</div>
      <el-table
        v-loading="loading"
        :data="list"
        stripe
        max-height="500"
        fit
        highlight-current-row
      >
        <el-table-column label="序号" width="95" align="center">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column
          label="项目名称"
          prop="name"
          align="center"
        ></el-table-column>
        <el-table-column
          label="应用服务器"
          prop="server"
          align="center"
        ></el-table-column>
        <el-table-column
          label="数据库"
          prop="db"
          align="center"
        ></el-table-column>
        <el-table-column
          label="定时任务"
          prop="mission"
          align="center"
        ></el-table-column>
        <el-table-column
          label="业务监控"
          prop="service"
          align="center"
        ></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <!--<el-button type="success" size="mini" @click="edit(scope.row)">修改</el-button>-->
            <el-button
              type="success"
              @click="updateProjectById(scope.row.id)"
              size="mini"
              >修改</el-button
            >
            <!--<el-button type="danger" size="mini" @click="del(scope.row.id)">删除</el-button>-->
            <el-button type="danger" size="mini" @click="open(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="page-box">
        <el-pagination
          :current-page="searchField.page"
          :page-sizes="[10, 15, 20, 25, 30, 50]"
          :page-size="searchField.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import {
  getProjectList,
  updateProjectById,
  projectadd,
  updateProjectdeleteById,
  projectupdate,
} from "@/api/table";

// eslint-disable-next-line no-unused-vars
export default {
  data() {
    return {
      list: null,
      loading: true,
      total: 10,
      searchField: {
        page: 1,
        limit: 15,
      },
      dialogFormVisible: false,
      dialogUpdateFormVisible: false,
      form: {
        id: "",
        name: "",
        server: "",
        db: "",
        mission: "",
        service: "",
        number: "",
        provider: "",
        tel: "",
        extention: "",
        address: "",
        is_delete: "",
      },
      formLabelWidth: "120px",
    };
  },
  created() {
    this.initListData();
  },
  methods: {
    // 查看
    showDetail(row) {
      console.log(row);
    },
    // 编辑
    edit(row) {
      console.log(row);
    },
    // 删除监控项目
    del(id) {
      console.log(id);
      updateProjectdeleteById(id).then((res) => {
        var flag = res.flag;
        if (flag == true) {
          this.$message.success("删除数据成功");
          this.initListData();
        } else {
          this.$message.error("删除数据失败");
        }
      });
    },
    initListData() {
      this.loading = true;
      console.log(this.searchField);
      getProjectList(this.searchField).then((res) => {
        this.list = res.data.rows;
        this.total = res.data.total;
        this.loading = false;
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.searchField.limit = val;
      this.pagingQuery();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      console.log(`当前页: ` + val);
      this.searchField.page = val;
      this.pagingQuery();
    },
    // 初始化dialog弹框的内容
    initDialogContent() {
      this.form = {
        name: "",
        server: "",
        db: "",
        mission: "",
        service: "",
        number: "",
        provider: "",
        tel: "",
        extention: "",
      };
    },
    addProjectPre() {
      this.dialogFormVisible = true;
      this.form = {
        id: "",
        name: "",
        server: "",
        db: "",
        mission: "",
        service: "",
        number: "",
        provider: "",
        tel: "",
        extention: "",
        address: "",
        is_delete: "",
      };
    },
    // 添加项目的dialog弹框
    addProject(project) {
      if (this.ipCheck) {
        projectadd(project).then((res) => {
          this.dialogFormVisible = false;
          this.initDialogContent();
          var flag = res.flag;
          if (flag == true) {
            this.$message.success("添加数据成功");
            this.initListData();
          } else {
            this.$message.error("添加数据失败");
            this.initDialogContent();
          }
        });
      } else {
        this.$message.error("请填写正确的ip");
      }
    },
    // 删除确认的提示框
    open(id) {
      this.$confirm("此操作将永久删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.del(id);
      });
    },
    // 根据id查询项目进行回显
    updateProjectById(id) {
      // 根据id查询数据并回显
      updateProjectById(id).then((res) => {
        this.dialogUpdateFormVisible = true;
        this.form = res.data;
      });
    },
    // 更新项目
    updateProject(project) {
      projectupdate(project)
        .then((res) => {
          this.dialogUpdateFormVisible = false;
          var flag = res.flag;
          if (flag == true) {
            this.$message.success("更新数据成功");
            this.initListData();
          } else {
            this.$message.error("添加数据失败");
            this.initDialogContent();
          }
        })
        .finally(this.initDialogContent);
    },
    // ip 校验
    ipCheck: function () {
      var reg =
        /^91\d{2}|2[0-4]\d|25[0-5][1-9]\d|[1-9](\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)){3}$/;
      var re = new RegExp(reg);
      // eslint-disable-next-line eqeqeq
      if (this.form.address == null || this.form.address == "") {
        this.$message({
          type: "warning",
          message: "ip不可为空！ ",
          /*duration: 10000,*/
          showClose: true,
        });
        return false;
      }

      if (!re.test(this.form.address)) {
        this.$message({
          type: "warning",
          message: "请填写有效的IP地址！ ",
          /*duration: 10000,*/
          showClose: true,
        });
        return false;
      }
    },
    // 分页查询函数
    pagingQuery: function () {
      getProjectList(this.searchField).then((res) => {
        this.list = res.data.rows;
        this.total = res.data.total;
        this.loading = false;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.redColor {
  color: red;
}

.search-box {
  margin-bottom: 15px;

  .search-hd {
    border-left: 2px solid #00a73c;
    padding: 10px;
    margin-bottom: 10px;
  }

  .search-bd {
    .el-button--small {
      margin-top: 3px;
    }
  }
}

.main-box {
  .main-hd {
    height: 40px;
    line-height: 40px;
    background: #00a73c;
    color: #fff;
    padding-left: 10px;
  }

  .page-box {
    padding: 10px;
    text-align: center;
  }
}
</style>
